﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GeoSketch2.Core
{
    public partial class PictureManagerForm : Form
    {
        #region Fields
        string CurrentDirectory;

        List<ThumbnailBox> Thumbnails;

        /// <summary>
        /// The full filename of the selected picture
        /// </summary>
        public string SelectedPictureFullFilename; 
        #endregion

        #region Constructor
        /// <summary>
        /// A picture manager to easily pick a picture
        /// </summary>
        /// <param name="Directory">The directory path which contains the picures</param>
        public PictureManagerForm(string Directory)
        {
            InitializeComponent();

            this.CurrentDirectory = Directory;
            this.Thumbnails = new List<ThumbnailBox>();
            this.Resize += new EventHandler(PictureManagerForm_Resize);

            LoadPictures();
        } 
        #endregion

        #region Events
        void PictureManagerForm_Resize(object sender, EventArgs e)
        {
            LoadPictures();
        }

        /// <summary>
        /// The form painting event
        /// </summary>
        protected override void OnPaint(PaintEventArgs e)
        {
            LoadPictures();
            base.OnPaint(e);
        }

        void Thumbnail_DoubleClick(object sender, EventArgs e)
        {
            ThumbnailBox Sender = (ThumbnailBox)((Panel)sender).Tag;
            this.SelectedPictureFullFilename = Sender.FullFileName;
            this.Hide();
        }

        private void addPictureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            DialogResult dr = ofd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                File.Copy(ofd.FileName, CurrentDirectory + "\\" + Path.GetFileName(ofd.FileName), true);
                if (File.Exists(Path.GetDirectoryName(ofd.FileName) + "\\" + Path.GetFileNameWithoutExtension(ofd.FileName) + ".gsp"))
                {
                    File.Copy(Path.GetDirectoryName(ofd.FileName) + "\\" + Path.GetFileNameWithoutExtension(ofd.FileName) + ".gsp",
                    CurrentDirectory + "\\" + Path.GetFileNameWithoutExtension(ofd.FileName) + ".gsp", true);
                }
                LoadPictures();
            }

        } 
        #endregion

        #region Methods
        void LoadPictures()
        {
            FileInfo[] Files = Tools.GetFilesFromExtensions(this.CurrentDirectory, Vars.Setup.Forms.PictureManager.FilesExtension);
            Files = FilterFiles(Files);
            CheckThumbnails(Files);

            Thumbnails.Clear();
            foreach (FileInfo FI in Files)
            {
                Thumbnails.Add(new ThumbnailBox(FI.FullName, new Size(Vars.Setup.Forms.PictureManager.ThumbnailsSize, Vars.Setup.Forms.PictureManager.ThumbnailsSize)));
            }
            RearrangeThumbnails();
        }

        FileInfo[] FilterFiles(FileInfo[] Files)
        {
            List<FileInfo> FilesFiltered = new List<FileInfo>();

            foreach (FileInfo FI in Files)
            {
                if (!FI.Name.EndsWith("_thumbnail" + FI.Extension))
                {
                    FilesFiltered.Add(FI);
                }
            }

            return FilesFiltered.ToArray();
        }

        void CheckThumbnails(FileInfo[] Files)
        {
            FileInfo TempFileInfo;
            Bitmap TempBitmap;
            RectangleD TempRectangleD;

            foreach (FileInfo FI in Files)
            {
                TempFileInfo = new FileInfo(FI.DirectoryName + "\\" + Path.GetFileNameWithoutExtension(FI.Name) + "_thumbnail.jpg");
                if (!TempFileInfo.Exists)
                {
                    TempBitmap = new Bitmap(FI.FullName);
                    TempRectangleD = Tools.FitRectangleIntoRectangle(new RectangleD(0, 0, Vars.Setup.Forms.PictureManager.ThumbnailsSize, Vars.Setup.Forms.PictureManager.ThumbnailsSize), new RectangleD(0, 0, TempBitmap.Width, TempBitmap.Height));
                    TempBitmap.GetThumbnailImage((int)TempRectangleD.Width, (int)TempRectangleD.Height, null, IntPtr.Zero).Save(FI.DirectoryName + "\\" + Path.GetFileNameWithoutExtension(FI.Name) + "_thumbnail.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
        }

        void RearrangeThumbnails()
        {
            int StartX = this.ClientRectangle.X + 6, StartY = this.ClientRectangle.Y + 6;
            int CurrentX = StartX, CurrentY = StartY;

            this.panel1.Controls.Clear();

            foreach (ThumbnailBox t in Thumbnails)
            {
                t.Location = new Point(CurrentX, CurrentY);

                CurrentX += t.Rectangle.Width + Vars.Setup.Forms.PictureManager.SpaceBetweenThumbnails;

                if (CurrentX + t.Rectangle.Width + Vars.Setup.Forms.PictureManager.SpaceBetweenThumbnails + 12 > this.panel1.ClientRectangle.Width)
                {
                    CurrentX = StartX;
                    CurrentY += t.Rectangle.Height + Vars.Setup.Forms.PictureManager.SpaceBetweenThumbnails;
                }

                this.panel1.Controls.Add(t.DisplayPanel);
                this.panel1.Controls.Add(t.FileNameLabel);
                t.DisplayPanel.DoubleClick += new EventHandler(Thumbnail_DoubleClick);
            }
        }
        #endregion 
    }
}
